package good

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/good"
	goodReq "github.com/flipped-aurora/gin-vue-admin/server/model/good/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type LsGoodApi struct{}

// CreateLsGood 创建lsGood表
// @Tags LsGood
// @Summary 创建lsGood表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body good.LsGood true "创建lsGood表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /lsGood/createLsGood [post]
func (lsGoodApi *LsGoodApi) CreateLsGood(c *gin.Context) {
	var lsGood good.LsGood
	err := c.ShouldBindJSON(&lsGood)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	lsGood.CreatedBy = utils.GetUserID(c)
	err = lsGoodService.CreateLsGood(&lsGood)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteLsGood 删除lsGood表
// @Tags LsGood
// @Summary 删除lsGood表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body good.LsGood true "删除lsGood表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /lsGood/deleteLsGood [delete]
func (lsGoodApi *LsGoodApi) DeleteLsGood(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	err := lsGoodService.DeleteLsGood(ID, userID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteLsGoodByIds 批量删除lsGood表
// @Tags LsGood
// @Summary 批量删除lsGood表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /lsGood/deleteLsGoodByIds [delete]
func (lsGoodApi *LsGoodApi) DeleteLsGoodByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	err := lsGoodService.DeleteLsGoodByIds(IDs, userID)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateLsGood 更新lsGood表
// @Tags LsGood
// @Summary 更新lsGood表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body good.LsGood true "更新lsGood表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /lsGood/updateLsGood [put]
func (lsGoodApi *LsGoodApi) UpdateLsGood(c *gin.Context) {
	var lsGood good.LsGood
	err := c.ShouldBindJSON(&lsGood)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	lsGood.UpdatedBy = utils.GetUserID(c)
	err = lsGoodService.UpdateLsGood(lsGood)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindLsGood 用id查询lsGood表
// @Tags LsGood
// @Summary 用id查询lsGood表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询lsGood表"
// @Success 200 {object} response.Response{data=good.LsGood,msg=string} "查询成功"
// @Router /lsGood/findLsGood [get]
func (lsGoodApi *LsGoodApi) FindLsGood(c *gin.Context) {
	ID := c.Query("ID")
	relsGood, err := lsGoodService.GetLsGood(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(relsGood, c)
}

// GetLsGoodList 分页获取lsGood表列表
// @Tags LsGood
// @Summary 分页获取lsGood表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query goodReq.LsGoodSearch true "分页获取lsGood表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /lsGood/getLsGoodList [get]
func (lsGoodApi *LsGoodApi) GetLsGoodList(c *gin.Context) {
	var pageInfo goodReq.LsGoodSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := lsGoodService.GetLsGoodInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

func (lsGoodApi *LsGoodApi) GetTypeGroups(c *gin.Context) {
	var pageInfo goodReq.LsGoodSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := lsGoodService.GetTypesList()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetLsGoodPublic 不需要鉴权的lsGood表接口
// @Tags LsGood
// @Summary 不需要鉴权的lsGood表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /lsGood/getLsGoodPublic [get]
func (lsGoodApi *LsGoodApi) GetLsGoodPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	lsGoodService.GetLsGoodPublic()
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的lsGood表接口信息",
	}, "获取成功", c)
}
